﻿using System.Net.Mail;

namespace GiveAQuiz.Web.Messenger
{
  /// <summary>
  /// This class implements the IMessageSender interface
  /// by using Smtp to send out the message.
  /// </summary>
  public class SmtpMessageSender : IMessageSender
  {
    /// <summary>
    /// Creates an instance of the SmtpMessageMessage class
    /// using the SmtpClient configured in the config file.
    /// </summary>
    public SmtpMessageSender()
      : this(new SmtpClient())
    {
    }

    /// <summary>
    /// Creates an instance of the SmtpMessageMessage class
    /// using the specified SmtpClient.
    /// </summary>
    /// <param name="smtpClient"></param>
    public SmtpMessageSender(SmtpClient smtpClient)
    {
      this.SmtpClient = smtpClient;
      this.SmtpClient.EnableSsl = true;
    }

    public SmtpClient SmtpClient
    {
      get;
      private set;
    }

    /// <summary>
    /// Send out a message using Smtp.
    /// </summary>
    /// <param name="from">The from e-mail address.</param>
    /// <param name="to">The to e-mail address.</param>
    /// <param name="subject">The e-mail subject line.</param>
    /// <param name="body">The message body.</param>
    public void SendMessage(string from, string to, string subject, string body)
    {
      var mailMsg = new MailMessage(from, to, subject, body);
      // make sure we support sending HTML in the message body
      mailMsg.IsBodyHtml = true; 
      SmtpClient.Send(mailMsg);
    }
  }
}